#include "netdisk.h"
void get_all_info(char *path, char *result, int ntfd)
{
    DIR *dir = opendir(path);
    if (dir == NULL)
    {
        char error[1024];
        memset(error, 0, sizeof(error));
        char e1[] = "The address \"";
        char e2[] = "\" does not exist.\n";
        strcat(error, e1);
        strcat(error, path);
        strcat(error, e2);
        strcat(result, error);
        return;
    }
    struct dirent *fileinfo;
    char dd[] = "DIRECTORY : ";
    strcat(result, dd);
    strcat(result, path);
    strcat(result, "\n");
    strcat(result, "*************************************************************\n");
    char dn[] = "File Name = ";
    char dt[] = "File Type = ";

    while ((fileinfo = readdir(dir)) != NULL)
    {
        strcat(result, dn);
        strcat(result, fileinfo->d_name);
        if (strlen(fileinfo->d_name) < 4)
        {
            strcat(result, "\t\t\t");
        }
        else if (strlen(fileinfo->d_name) >= 4 && strlen(fileinfo->d_name) < 12)
        {
            strcat(result, "\t\t");
        }
        else if (strlen(fileinfo->d_name) >= 12 && strlen(fileinfo->d_name) < 20)
        {
            strcat(result, "\t");
        }
        strcat(result, dt);
        if (fileinfo->d_type == DT_UNKNOWN)
        {
            strcat(result, "unknow type");
        }
        else if (fileinfo->d_type == DT_REG)
        {
            strcat(result, "regular file");
        }
        else if (fileinfo->d_type == DT_DIR)
        {
            strcat(result, "directory file");
        }
        else if (fileinfo->d_type == DT_FIFO)
        {
            strcat(result, "pipe file");
        }
        else if (fileinfo->d_type == DT_SOCK)
        {
            strcat(result, "socket file");
        }
        else if (fileinfo->d_type == DT_CHR)
        {
            strcat(result, "character device file");
        }
        else if (fileinfo->d_type == DT_BLK)
        {
            strcat(result, "block device File");
        }
        else if (fileinfo->d_type == DT_LNK)
        {
            strcat(result, "link file");
        }

        strcat(result, "\n");
    }
    strcat(result, "*************************************************************\n");
}

void ls_fun(char *arg, int ntfd, Stack_t *dir)
{
    // 变量声明
    char result[8192]; //-用于存储最终结果
    char path[256];    //-用于存储目录的位置

    // 判断是否无参
    if (arg == NULL) //-无参
    {
        travel_stack(*dir, path);
        get_all_info(path, result, ntfd);
    }
    else //-有参
    {
        // 判断是是几个参数
        char *cur = arg;
        char args[100][1024]; //-i=行，j=列
        memset(args, 0, sizeof(args));
        int i = 0;
        int j = 0;
        bool meetspace = 0;
        bool meetcharacter_1 = 0;
        bool meetcharacter_2 = 0;
        while (*cur != '\0')
        {
            if (meetspace && meetcharacter_1 && meetcharacter_2)
            {
                i++;
                j = 0;
                meetcharacter_1 = 0;
                meetcharacter_2 = 0;
                meetspace = 0;
            }
            else if (isspace(*cur))
            {
                cur++;
                if (meetcharacter_1 == 1)
                {
                    meetspace = 1;
                }
            }
            else
            {
                if (meetspace == 1)
                {
                    meetcharacter_2 = 1;
                    continue;
                }
                args[i][j] = *cur;
                j++;
                cur++;
                meetcharacter_1 = 1;
            }
        }
        // i是几，就说明是j+1个参数

        // 判断是绝对地址还是相对地址，并获取对应的绝对地址
        for (int line = 0; line <= i; line++)
        {
            char real_path[4096];
            memset(real_path, 0, sizeof(real_path));
            char *cur = args[line];

            onePath(args[line], real_path, dir);
            get_all_info(real_path, result, ntfd);
            
        }
    }
    int ret = 0;
    send(ntfd, &ret, sizeof(int), MSG_NOSIGNAL);
    int  result_length=strlen(result);
    send(ntfd, &result_length, sizeof(int), MSG_NOSIGNAL);
    send(ntfd, result, result_length, MSG_NOSIGNAL);
}
